//
//  LBCategoryTopHeaderReusableView.m
//  jslj
//
//  Created by luckyRoy on 2017/10/24.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCategoryTopHeaderReusableView.h"
#import "LBCategoryModel.h"
#import "LBCategorySectionView.h"
#import <Masonry.h>
#import "LBCategoryHeaderViewModel.h"

@interface LBCategoryTopHeaderReusableView ()

@property (nonatomic, strong) LBCategorySectionView *headerView;

@end

@implementation LBCategoryTopHeaderReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
    [self addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.right.equalTo(self);
        make.bottom.offset(-5);
    }];
}

#pragma mark - click

#pragma mark - setter

- (void)fac_updateUIViewByModel:(id)viewModel
{
    LBCategoryHeaderViewModel *model = viewModel;
    
    [self.headerView.editorButton setHidden:model.isHideEditorButton];
    [self.headerView setIsEditor:model.isEditor];
    [self.headerView updateUIByTopTitle:model.title OtherTitle:model.content categoryUrl:model.categoryUrl];
}

#pragma mark - getter

- (LBCategorySectionView *)headerView
{
    if (!_headerView) {
        __weak __typeof(&*self)weakSelf = self;
        _headerView = [[LBCategorySectionView alloc]init];
        [_headerView setViewBlock:^(BOOL isShowEditor) {
            if (isShowEditor) {
                //编辑
                if (weakSelf.fac_clickBlock) {
                    weakSelf.fac_clickBlock(0);
                }
            } else {
                if (weakSelf.fac_clickBlock) {
                    weakSelf.fac_clickBlock(1);
                }
            }
        }];
    }
    return _headerView;
}

+ (CGFloat)fac_heightForViewByModel:(id)viewModel
{
    LBCategoryHeaderViewModel *model = viewModel;

    CGFloat totalHeight = 0;

    if (!model) {
        return 0;
    }
    
    if (model.title) {
        totalHeight += [LBCategorySectionView heightForViewTopState:YES categoryState:NO];
    }
    
    if (model.content) {
        totalHeight += [LBCategorySectionView heightForViewTopState:NO categoryState:YES];
    }
    
    totalHeight += 10;
    
    return totalHeight;
}

@end
